﻿<?php header('Content-Type: text/html; charset=utf8'); ?>
<html>
    <head>
        <title>Stocks</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script src="jsrender.js" type="text/javascript"></script>
        <script type="text/javascript">
            document.onkeydown=function(e) {
                if(e.keyCode == 39) {
                    if(document.getElementById("next") !== null) {
                        document.location = document.getElementById("next").href; 
                    }
                } else if(e.keyCode == 37) {
                    if(document.getElementById("prev") !== null) {
                        document.location = document.getElementById("prev").href; 
                    }
                } 
            }
        </script>
        <script>
            $(document).ready(function(){
                $("#depo").html("<img src='load.gif'>");
				var tmp = "?"+new Date().getTime(); 
                var data = <?php echo file_get_contents("http://127.0.0.1:8080/myDepo.json",0,null,null);?>;
			
                        var info = data.pop();
                        $("#rate").html("Idag: "+info.value);
						$("#time").html("  uppdaterad "+info.latesttime);
                        var headers = "<tr><th>Värdepapper&nbsp;&nbsp;&nbsp;</th><th>Senast betalt&nbsp;&nbsp;&nbsp;</th><th>+/- idag&nbsp;&nbsp;&nbsp;</th><th>% idag&nbsp;&nbsp;&nbsp;</th><th>Inköp&nbsp;&nbsp;&nbsp;</th><th>Tot. värde&nbsp;&nbsp;&nbsp;</th><th>+/- sedan köp&nbsp;&nbsp;&nbsp;</th><th>% sedan köp&nbsp;&nbsp;&nbsp;</th></tr>";
                        $("#depo").html("").append("<table/>");
                        $("#depo table:first").append("<thead/>").append(headers);
                        $("#depo table:first").append("<tbody/>").append(
                            $("#stock_template").render( data )
                        );
            });
            $('#depo').click(function(){
                //$("#depo").html("<button type='button'>Laddar värden...</button>")
            });
            $.views.registerHelpers({
                isPos: function( val ) {
                    return parseFloat( val.replace(",",".") ) >= 0;
                }
            });
        </script>
        <script id="stock_template" type="text/x-jquery-tmpl">
            <tr class="stock">
                <td>{{=stock}}</td>
                <td>{{=latestPay}}</td>
                <td>{{=day}}</td>
                {{#if $ctx.isPos(dayRate)}}
                    <td style='color:green;'>{{=dayRate}}%</td>
                {{else}}
                    <td style='color:red;'>{{=dayRate}}%</td>
                {{/if}}
                <td>{{=bought}}</td>
                <td>{{=current}}</td>
                <td>{{=total}}</td>
                {{#if $ctx.isPos(totalRate)}}
                    <td style='color:green;'>{{=totalRate}}%</td>
                {{else}}
                    <td style='color:red;'>{{=totalRate}}%</td>
                {{/if}}
            </tr>
        </script>
        <style type="text/css">
            body {
                font-family: Arial, Helvetica, Geneva; 
            }
        </style>
    </head>
    <body>
        <?php
        $files = scandir('./graphs/highlow');
        $nr = $_GET['nr'];
        if (!isset($nr)) {
            echo "<h2>Aktieinnehav</h2>";
            echo "<span id='rate'></span>";
			echo "<span id='time'></span>";
            echo "<div styel='height:105px;' id='depo'></div>";
            echo "<h2>Trevliga stockar</h2>\n";
            $jsonurl = "http://127.0.0.1:8080/myStocks.php";
            $json = file_get_contents($jsonurl,0,null,null);
            $json_output = json_decode($json);
            foreach ($json_output as $name ) {
                echo "<a href='threemonthly.php?name=$name'>$name<br></a>\n";
            }
            echo "<h2>Otrevliga stockar</h2>\n";
            foreach($files as $i => $file){
                if ($i > 1) {
                    $name = str_replace('.gif','',$file);
                    echo "<a href='?nr=$i'>$name</a><br>\n";
                }
            } 
        } else {
            //$name = str_replace('.gif','',$files[$nr]);
            echo "<a id='back' href='?'>Tillbaka</a>\n";
            $nextNr = $nr+1;
            $prevNr = $nr-1;
            echo "<a href='?nr=$nextNr'><img src='/graphs/highlow/".$files[$nr]."' width='1024' height='500'></a><br>\n";
            echo "<a id='next' href='?nr=$nextNr'>Framåt</a><br>\n";
            echo "<a id='prev' href='?nr=$prevNr'>Bakåt</a>\n";
        }
        ?>
    </body>
</html>
